package com.beimihu.until;

import java.io.Console;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class Sha256WithRsa {
    public static boolean doCheck(String content, String sign, String publicKey) {
        if (sign == null) {
            return false;
        }
        if (publicKey == null) {
            return false;
        }

        try {
            // 清理 Base64 字符串中的非法字符（空格、换行等）
            publicKey = publicKey.replaceAll("\\s+", "");
            sign = sign.replaceAll("\\s+", "");

            // 解码公钥
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            byte[] encodedKey = Base64.getDecoder().decode(publicKey); // 使用Base64的getDecoder()
            PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));

            // 验证签名
            Signature signature = Signature.getInstance("SHA256WithRSA");
            signature.initVerify(pubKey);
            signature.update(content.getBytes(StandardCharsets.UTF_8));

            // 解码签名
            byte[] bsign = Base64.getDecoder().decode(sign); // 使用Base64的getDecoder()
            return signature.verify(bsign);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args) {
        String content = "{\"country\":\"JP\",\"purchaseToken\":\"00000193049572e5fcc9eb085e43b539b1414ae799cec6741100ae0ed6f14b6a1c3fa98973a82042x4a50.5.112413763\",\"consumptionState\":0,\"payOrderId\":\"sandboxAd3601fd24cafb2192f0dd11d2df5863\",\"productId\":\"tu_zs1\",\"orderId\":\"20241107110513996459607e45.112413763\",\"kind\":0,\"developerPayload\":\"111111zsh5.104_1|1\",\"applicationIdString\":\"112413763\",\"confirmed\":0,\"productName\":\"648档位钻石\",\"purchaseType\":0,\"sdkChannel\":\"1\",\"payType\":\"34\",\"price\":304500,\"purchaseTime\":1730948723000,\"currency\":\"JPY\",\"packageName\":\"com.huawei.fastapp.dev\",\"applicationId\":112413763,\"autoRenewing\":false,\"purchaseState\":0,\"purchaseTimeMillis\":1730948723000}";
        String sign = "D7Km6VT3U+SGCiROK3CNGEPPV7v+ZiZtW6C6tpfah/hU264TVVw6beLVMv7HyJnLDiWkR+2g+N/zoe0TnS33llPUcklhKvZ5KKVnFuycQv0lQEVSbgnwpEzH2hMtEnG9uTiZk7FmC/vER8zzrSrGnUWe5PIHAI+jjOD4TZoeSqIyprEMUHsIjSe9sgixrtopv3YuUVjRucP1vLGM3Z5vU/DPlE8N+EB8hTEaVoLRDhWangwuNpImte8XcpwoB8DmLi3l0HCqCpAVOU6isPp6+9NOeQAzbwxKQMVKzXmxx6x/eQqWKWwXi2phTNsmH6qc1A1kDOk9gHtycKw4ENgClgS4zWfaaOdLpDVfP7fP+r1dbP+xrtwRZkcmzRNV/GrpP1w7UrJscpWKbQ0gAQ+/6gV0K0zTz8SzJ8eGDm5Y2JFxUzqSZgFUCzDchne3VpCpV9xldT2H1bDpGzGi4CXrBKKTEFh125ozHJLuqDDxTWxGGxH83RiFoykpGc32F9Yp";
        String publicKey = "MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAm99hjCrNyG6NIP629+v0QlDZKqOibt3sOyip+hyvHeHUjXzem/9AXhRwq0IF99xUeRpHfgUEO8r8cv1y45NL4jvD1ZLiEWPVvigp+vySqF1rbVUXRqtAa6qjXtiqrA74Oo7eX0At1bkyDabSx6skBfgbWc3HWw/WuJlXu6Q1osC1MT6ssBvxgrMhJ4QF6JjxglN5KwKV3AT9Z4xQwqr33PFU9IQN3GEmRm83tP96ptLlGvm37W9ca+OSaWigdxZL1PplBXWN9+DbcI4UI3YTerR3xmlVJEYcY0ufXAv2+BkD52wPYl3WpBEfhN5LfTmS+3WdTqs/9JUS1eaN8+sAsHxsNhlOThClIrMcecQFlAT7SK2nYw1O4ukuPYjoiWKFW6dHqYvqCkcphMrwyUNOXzWDHpb/hx42wajhR7690AFj4nEl3YiZPZ6uWI+pFjptUlj90nt7PRWZMdwLyPdYxDlVFXnBRC6/lZSXZUFoijt0SL7pdlCKJJ8cvAad7Y0pAgMBAAE=";

        boolean result = doCheck(content, sign, publicKey);
        System.out.println(result);
    }
}
